/**
 * 
 */
package sniper.wizard;

import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IWorkbench;

import sniper.targets.SnippetHolder;

/**
 * Class representing the first page of the wizard
 * 
 * @author Valerio Schiavoni
 *
 */
public class SniperFirstPage extends WizardPage implements Listener {

	IWorkbench workbench;
	IStructuredSelection selection;
	
	final static String[] availableSites ={"http://rafn.net/pastebin", "http://papernapkin.org/pastebin"};
	
	
	//	widgets on this page 
	Combo sites;
	Text sourceCodeTextArea;
	
	public SniperFirstPage(IWorkbench workbench, IStructuredSelection selection) {
		super("Page1");
		setTitle("Sniper - Target site selection");
		setDescription("Select the site to publish the selected source code.");
		this.workbench = workbench;
		this.selection = selection;
	}
	
	public void handleEvent(Event event) {
		//XXX handle events more properly
		setPageComplete(isPageComplete());
		getWizard().getContainer().updateButtons();
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
	 */
	public void createControl(Composite parent) {

		 // create the composite to hold the widgets
		GridData gd;
		Composite composite =  new Composite(parent, SWT.NULL);

	    // create the desired layout for this wizard page
		GridLayout gl = new GridLayout();
		int ncol = 4;
		gl.numColumns = ncol;
		composite.setLayout(gl);
		
		 // create the widgets.
		 // available paste-bin sites
		new Label (composite, SWT.NONE).setText("Available target sites:");						
		sites = new Combo(composite, SWT.BORDER | SWT.READ_ONLY);
		gd = new GridData();
		gd.horizontalAlignment = GridData.BEGINNING;
		//gd.widthHint = 100;
		sites.setLayoutData(gd);
		sites.setItems(availableSites);		
		sites.setText(sites.getItem(0)); // 0 based indexes
		
		createLine(composite, ncol);
		
		sourceCodeTextArea = new Text(composite, SWT.MULTI | SWT.READ_ONLY);
		sourceCodeTextArea.setText(SnippetHolder.getSNIPPET());
		final GridData sourceCodeTextAreagridData = new GridData(GridData.FILL_BOTH);
		sourceCodeTextAreagridData.horizontalSpan = ncol;
		sourceCodeTextArea.setLayoutData(sourceCodeTextAreagridData);
		
		 // set the composite as the control for this page
		setControl(composite);		
		addListeners();
	}
	
	/**
	 * add listeners to this page widgets
	 */
	private void addListeners() {
		sites.addListener(SWT.Selection, this);
	}
	private void createLine(Composite parent, int ncol) 
	{
		Label line = new Label(parent, SWT.SEPARATOR|SWT.HORIZONTAL|SWT.BOLD);
		GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
		gridData.horizontalSpan = ncol;
		line.setLayoutData(gridData);
	}	

	/*
	 * Sets the completed field on the wizard class when all the information 
	 * is entered and the wizard can be completed
	 */	 
	@Override
	public boolean isPageComplete()
	{
		SniperWizard wizard = (SniperWizard)getWizard();
		
		wizard.sniperCompleted = true;
			
		return true;
	}
	
	/**
	 * @see IWizardPage#canFlipToNextPage()
	 */
	public boolean canFlipToNextPage()
	{
		if (getErrorMessage() != null) return false;
	
			return true;
	
	}
}
